﻿using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using zhangmenren.IService;
using zhangmenren.Models.help;
using zhangmenren.Untils;

namespace zhangmenren.Controllers
{
	public class MyInfoController : Controller
	{
		private IUserService userService;
		public MyInfoController(IUserService _userService)
		{
			userService = _userService;
		}


		public async Task<IActionResult> Index()
		{
			var user = QTCookie.GetCookie<UserCook>(HttpContext);
			if (user == null)
			{
				return Redirect("/Login/Index");
			}
			var entity = await userService.FindById(user.Id);
			if (string.IsNullOrEmpty(entity.UserName))
			{
				return Redirect("/Login/Index");
			}
			return View(entity);
		}

		public async Task<IActionResult> UpPwd()
		{
			var user = QTCookie.GetCookie<UserCook>(HttpContext);
			if (user == null)
			{
				return Redirect("/Login/Index");
			}
			var entity = await userService.FindById(user.Id);
			if (string.IsNullOrEmpty(entity.UserName))
			{
				return Redirect("/Login/Index");
			}
			return View(entity);
		}

		public async Task<IActionResult> UpUserName()
		{
			var user = QTCookie.GetCookie<UserCook>(HttpContext);
			if (user == null)
			{
				return Redirect("/Login/Index");
			}
			var entity = await userService.FindById(user.Id);
			if (string.IsNullOrEmpty(entity.UserName))
			{
				return Redirect("/Login/Index");
			}
			return View(entity);
		}
        public IActionResult UpMobile()
        {
            var user = QTCookie.GetCookie<UserCook>(HttpContext);
            if (user == null)
            {
                return Redirect("/Login/Index");
            }
            return View();
        }

        public IActionResult UpJianJie()
        {
            var user = QTCookie.GetCookie<UserCook>(HttpContext);
            if (user == null)
            {
                return Redirect("/Login/Index");
            }
            return View();
        }


        public IActionResult UpAddress()
        {
            var user = QTCookie.GetCookie<UserCook>(HttpContext);
            if (user == null)
            {
                return Redirect("/Login/Index");
            }
            return View();
        }


        public IActionResult UpSignature()
        {
            var user = QTCookie.GetCookie<UserCook>(HttpContext);
            if (user == null)
            {
                return Redirect("/Login/Index");
            }
            return View();
        }




        public async Task<JsonResult> EditPwd(string oldPwd, string pwd)
		{
			var user = QTCookie.GetCookie<UserCook>(HttpContext);
			if (user == null)
			{
				return Json(new { state = "login", msg = "请重新登录" });
			}
			var entity = await userService.FindById(user.Id);

			if (entity.Pwd != Md5.MD5Str(oldPwd))
			{
				return Json(new { state = "fail", msg = "老密码错误" });
			}

			var result = await userService.SetPwd(Md5.MD5Str(pwd), entity.Id);
			return Json(new { state = "ok", msg = "修改成功" });
		}


		public async Task<JsonResult> EditHead(string head)
		{
			var user = QTCookie.GetCookie<UserCook>(HttpContext);
			if (user == null)
			{
				return Json(new { state = "login", msg = "请重新登录" });
			}
			var entity = await userService.FindById(user.Id);

			if (string.IsNullOrEmpty(head))
			{
				return Json(new { state = "fail", msg = "请上传图片" });
			}

			UserCook cook = new UserCook();
			cook.Head = head;
			cook.Id = entity.Id;
			cook.UserName = entity.UserName;
			var result = await userService.SetHead(head, user.Id);
			QTCookie.DeleteCookie(HttpContext);
			QTCookie.SaveCookie<UserCook>(HttpContext, cook);
			return Json(new { state = "ok", msg = "修改成功" });
		}


		public async Task<JsonResult> EditAddress(string address)
		{
			var user = QTCookie.GetCookie<UserCook>(HttpContext);
			if (user == null)
			{
				return Json(new { state = "login", msg = "请重新登录" });
			}
			
			if (string.IsNullOrEmpty(address))
			{
				return Json(new { state = "fail", msg = "请输入地址" });
			}

			var result = await userService.SetAddress(address, user.Id);
			return Json(new { state = "ok", msg = "修改成功" });
		}



		public async Task<JsonResult> EditUserName(string username)
		{
			var user = QTCookie.GetCookie<UserCook>(HttpContext);
			if (user == null)
			{
				return Json(new { state = "login", msg = "请重新登录" });
			}
			var entity = await userService.FindById(user.Id);

			if (string.IsNullOrEmpty(username))
			{
				return Json(new { state = "fail", msg = "请输入用户名" });
			}

			var isok = await userService.Find(username);

			if (!string.IsNullOrEmpty(isok?.UserName))
			{
				return Json(new { state = "fail", msg = "用户名存在" });
			}
			var result = await userService.SetUserName(username, entity.Id);
			UserCook cook = new UserCook();
			cook.Head = entity.Head;
			cook.Id = entity.Id;
			cook.UserName = username;
			QTCookie.DeleteCookie(HttpContext);
			QTCookie.SaveCookie<UserCook>(HttpContext, cook);
			return Json(new { state = "ok", msg = "修改成功" });
		}



		public async Task<JsonResult> EditMobile(string mobile)
		{
			var user = QTCookie.GetCookie<UserCook>(HttpContext);
			if (user == null)
			{
				return Json(new { state = "login", msg = "请重新登录" });
			}

			if (string.IsNullOrEmpty(mobile))
			{
				return Json(new { state = "fail", msg = "请输入手机号" });
			}
			var result = await userService.SetMobile(mobile, user.Id);
			return Json(new { state = "ok", msg = "修改成功" });
		}

		public async Task<JsonResult> EditMiaoShu(string miaoshu)
		{
			var user = QTCookie.GetCookie<UserCook>(HttpContext);
			if (user == null)
			{
				return Json(new { state = "login", msg = "请重新登录" });
			}

			if (string.IsNullOrEmpty(miaoshu))
			{
				return Json(new { state = "fail", msg = "请输入个性签名" });
			}

			var result = await userService.SetMaoShu(miaoshu, user.Id);
			return Json(new { state = "ok", msg = "修改成功" });
		}

        public async Task<JsonResult> EditJianJie(string jianjie)
        {
            var user = QTCookie.GetCookie<UserCook>(HttpContext);
            if (user == null)
            {
                return Json(new { state = "login", msg = "请重新登录" });
            }

            if (string.IsNullOrEmpty(jianjie))
            {
                return Json(new { state = "fail", msg = "请输入个性签名" });
            }

            var result = await userService.SetJianJie(jianjie, user.Id);
            return Json(new { state = "ok", msg = "修改成功" });
        }
    }
}